﻿using SIG.DAL.Dapper.Model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIG.DAL.Dapper.Repositories
{

    public class WorkCategoryRepository
    {
        private DbConnection _connection;
        public IEnumerable<WorkCategory> GetActiveItems()
        {
            IEnumerable<WorkCategory> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<WorkCategory>(new { Active = true });
            }
            return result;
        }

        public WorkCategory GetItemBySeoName(string seoName)
        {
            WorkCategory result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<WorkCategory>(new { SeoName = seoName }).FirstOrDefault();
            }
            return result;
        }

        public bool IsExistSeoName(string seoName)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                seoName = seoName.ToLower();
                result = _connection.RecordCount<WorkCategory>("Where SeoName = @SeoName", new { SeoName = seoName });
            }
            return result > 0;

        }
        public bool IsExistSeoName(string seoName, int id)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                seoName = seoName.ToLower();
                result = _connection.RecordCount<WorkCategory>("Where SeoName = @SeoName and Id <> @Id", new { SeoName = seoName, Id = id });
            }
            return result > 0;
        }

        public IEnumerable<WorkCategory> GetItems()
        {
            IEnumerable<WorkCategory> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<WorkCategory>();
            }
            return result;
        }

        public IEnumerable<WorkCategory> GetItemsByWorkId(int WorkId)
        {
            IEnumerable<WorkCategory> result;
            using (_connection = Utilities.GetOpenConnection())
            {               
                var sql = "SELECT  WorkCategorySet.* FROM WorkWorkCategorySet INNER JOIN WorkCategorySet ON WorkWorkCategorySet.CategoryId = WorkCategorySet.Id " +
                    "WHERE WorkWorkCategorySet.WorkId = @WorkId";                
                result = _connection.Query<WorkCategory>(sql, new { WorkId = WorkId });                
            }
            return result;

        }

        public IEnumerable<WorkCategory> GetListPagedItems(int pageIndex, int pageSize, string keyword, out int totalCount)
        {
            IEnumerable<WorkCategory> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                keyword = !string.IsNullOrEmpty(keyword) ? keyword.Replace("[", "[[]").Replace("%", "[%]") : keyword;
                string likeKeyword = "%" + keyword + "%";

                totalCount = _connection.RecordCount<WorkCategory>("where Title like @Title", new { Title = likeKeyword });
                result = _connection.GetListPaged<WorkCategory>(pageIndex, pageSize, "where Title like @Title", "Importance desc", new { Title = likeKeyword });
            }
            return result;
        }

        public WorkCategory GetById(int id)
        {
            WorkCategory result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Get<WorkCategory>(id);
            }
            return result;
        }
        public int? Create(WorkCategory model)
        {
            int? result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Insert(model);
            }
            return result;
        }

        public int Update(WorkCategory model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Update(model);
            }
            return result;
        }
        public int Delete(WorkCategory model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete(model);
            }
            return result;
        }

        public int Delete(int id)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete<WorkCategory>(id);
            }
            return result;
        }

        public object GetCategoriesWithParent()
        {
            throw new NotImplementedException();
        }
    }
}
